<?php
session_start();

ob_start();//for fb()
require_once("smarty/libs/Smarty.class.php");
include_once("fb.php");

// constants : default
define("DEBUG", true);

// constants : db
define("DB_HOST", "127.0.0.1");
define("DB_USER", "root");
define("DB_PASSWORD", "qhdksdusrntlf");
define("DB_NAME", "gru");
define("G_URL", "http://".$_SERVER['HTTP_HOST']);
define('CRLF', "\r\n");
define('G_FRONT_VERSION', "ver1/");

function s($v){
	fb($v);
}

function getSmarty(){
	$s = new Smarty();
	$s->template_dir  = 'tpls';
	$s->compile_dir   = 'tpls/tpls_c';
	$s->compile_check = true;
	//$smarty->debugging = false;
	$s->assign("header"		,$s->fetch(G_FRONT_VERSION."hd.htm"));
	$s->assign("footer"		,$s->fetch(G_FRONT_VERSION."ft.htm"));
	$s->assign("G_URL"		,G_URL);	
	return $s;
}

//biy.ly 로그
//http://api.bitly.com/v3/clicks?shortUrl=http%3A%2F%2Fbit.ly%2qH3xb0&login=han905&apiKey=R_bea239af52c0719908135a05fd821c49&hash=j3&format=xml

//bit.ly 짧은 도메인 만들기
function file_get_contents_curl($url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_URL, $url);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

function getShortURL($long_url){
	$bitly_login = "han905";
	$bitly_api_key = "R_bea239af52c0719908135a05fd821c49";
	$bitly_url = "http://api.bit.ly/v3/shorten";
	$request_url = $bitly_url . "?login=" . $bitly_login . "&apiKey=" . $bitly_api_key . "&longUrl=" . urlencode($long_url);
	$response = file_get_contents_curl($request_url);
	$response = json_decode($response);
	$short_url = $response->data->url;
	return $short_url;
}

//api limit 때문에 hansv905계정을 하나 만듦
function getShortURL2($long_url){
	$bitly_login = "hansv905";
	$bitly_api_key = "R_239d7ca393cbca9772932f60c6a7d9d7";
	$bitly_url = "http://api.bit.ly/v3/shorten";
	$request_url = $bitly_url . "?login=" . $bitly_login . "&apiKey=" . $bitly_api_key . "&longUrl=" . urlencode($long_url);
	$response = file_get_contents_curl($request_url);
	$response = json_decode($response);
	$short_url = $response->data->url;
	return $short_url;
}


function getShortURL_durl($long_url){
	$bitly_url = "http://durl.me/api/Create.do";
	$request_url = $bitly_url . "?type=json&longurl=". urlencode($long_url);
	$response = file_get_contents_curl($request_url);
	$response = json_decode($response);
	$short_url = $response->data->url;
	return $short_url;
	/*
	return value
	{
	"key":"bt2",
	"shortUrl":"http://durl.me/bt2",
	"status":"ok",
	"longUrl":"http://www.google.co.kr/search?complete=1&hl=ko&q=why+worry&lr=&aq=5&oq=why"
	}
	*/ 

}


//url포함 $cnt자 만들기
function cropCnt($str,$url,$cnt){	
	$cntURL = strlen($url);
	$limit = $cnt-$cntURL;
	return crop($str,$limit);
}
//글자자르기
function crop($string, $length = 80, $etc = '...')
{
	$s = iconv_substr($string, 0, $length, "utf-8");
	if (strlen($s) >= strlen($string))
        $etc = "";
    return $s . $etc;	
}


function datetimediff($rtime, $ctime = null, $option = null){
      if ($ctime) $cur_time = strtotime($ctime);
      else $cur_time = time();
      $ref_time = strtotime($rtime);

      $cur_date = floor($cur_time / 86400);
      $ref_date = floor($ref_time / 86400);

      $datetimediff = $cur_time - $ref_time;
      $datedist = $cur_date - $ref_date;
      $datediff = floor($datetimediff / 86400);
      $weekdiff = floor($datediff / 7);
      $timediff = $datetimediff % 86400;

      $hour = floor($timediff / 3600);
      $min = floor($timediff % 3600 / 60);
      $sec = floor($timediff % 3600 % 60);

      $result = "";
      if ($datedist>34) {
            $result = date("Y년 n월 j일", $ref_time);
      } else if ($weekdiff>0) {
            $result = $weekdiff . "주 전";
      } else {
            if ($datediff>0) {
                  $result = $datedist . "일 전";
            } else if ($timediff<=0) {
                  $result = "1초 전";
            } else {
                  if ($hour) $result = $hour . "시간";
                  else if ($min) $result = $min . "분";
                  else $result = $sec . "초";
                  if ($result) $result .= " 전";
            }
      }
      if ($option=='ALL') {
            $result = "";
            if ($datediff) $result .= ($result?" ":"") . $datediff."일";
            if ($hour) $result .= ($result?" ":"") . $hour."시간";
            if ($min) $result .= ($result?" ":"") . $min ."분";
            if ($sec) $result .= ($result?" ":"") . $sec . "초";
            $result .= " 전";
      }
      return $result;
}
function datetimediff2($rtime, $ctime = null, $option = null){
      if ($ctime) $cur_time = strtotime($ctime);
      else $cur_time = time();
      $ref_time = strtotime($rtime);

      $cur_date = floor($cur_time / 86400);
      $ref_date = floor($ref_time / 86400);

      $datetimediff = $cur_time - $ref_time;
      $datedist = $cur_date - $ref_date;
      $datediff = floor($datetimediff / 86400);
      $weekdiff = floor($datediff / 7);
      $timediff = $datetimediff % 86400;

      $hour = floor($timediff / 3600);
      $min = floor($timediff % 3600 / 60);
      $sec = floor($timediff % 3600 % 60);

      $result = "";
      if ($datedist>34 || $weekdiff>0) {
            $result = date("Y-n-j h:m:s", $ref_time);
      } else {
            if ($datediff>0) {
                  $result = $datedist . "일 전";
            } else if ($timediff<=0) {
                  $result = "1초 전";
            } else {
                  if ($hour) $result = $hour . "시간";
                  else if ($min) $result = $min . "분";
                  else $result = $sec . "초";
                  if ($result) $result .= " 전";
            }
      }
      if ($option=='ALL') {
            $result = "";
            if ($datediff) $result .= ($result?" ":"") . $datediff."일";
            if ($hour) $result .= ($result?" ":"") . $hour."시간";
            if ($min) $result .= ($result?" ":"") . $min ."분";
            if ($sec) $result .= ($result?" ":"") . $sec . "초";
            $result .= " 전";
      }
      return $result;
}

function debug_flush($msg)
{
	ob_end_clean();
    echo $msg.'<br/>';
    echo str_pad(' ',256);
    ob_flush();
    flush();
}

?>
